package cn.edu.gzu.chat.controller;

import cn.edu.gzu.chat.entity.MessageEntity;
import cn.edu.gzu.chat.feign.UserFeign;
import cn.edu.gzu.chat.mapper.MessageMapper;
import cn.edu.gzu.chat.result.Result;
import cn.edu.gzu.chat.service.MessageService;
import cn.edu.gzu.chat.vo.MessageList;
import cn.edu.gzu.chat.vo.MessageVo;
import cn.edu.gzu.chat.vo.UserVo;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jdk.nashorn.internal.runtime.linker.LinkerCallSite;
import lombok.Data;
import org.omg.CORBA.INTERNAL;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/chat")
public class MessageController {
    @Resource(name = "messageService")
    private MessageService messageService;
    @Resource
    private MessageMapper mapper;
    @Resource
    private UserFeign userFeign;

     @GetMapping("/info")
    public JSONObject getMessages(@RequestParam("userId") long user_id){
         QueryWrapper<MessageEntity> queryWrapper = new QueryWrapper<>();
         queryWrapper.eq("send_by",user_id).or().eq("send_to",user_id);
         queryWrapper.orderByDesc("create_time");
         List<MessageEntity> messageEntityList = messageService.list(queryWrapper);//查询到的聊天记录
         MessageList data = new MessageList(); //返回给前端的数据
         List<Long> loaded_user = new ArrayList<>();  //用来记录需要加载信息的对方用户
         for (MessageEntity msg: messageEntityList ) {
             Long sendBy = msg.getSendBy();
             Long sendTo = msg.getSendTo();
             MessageVo messageVo = new MessageVo();
             if (user_id != sendBy && !loaded_user.contains(sendBy)) {
                 loaded_user.add(sendBy);
                 System.out.println("用户id："+sendBy);
                 UserVo user_info = userFeign.getUserInfo(sendBy);
                 //用户信息
                 messageVo.setTitle(user_info.getNickname());
                 messageVo.setImg(user_info.getAvatarUrl());
                 //消息内容
                 messageVo.setContent(msg.getMessageText());
                 messageVo.setDate(msg.getCreateTime());
                 data.getMessages().add(messageVo);

             }else if(user_id != sendTo && !loaded_user.contains(sendTo)){
                 loaded_user.add(sendTo);
                 System.out.println("用户id："+sendTo);
                 UserVo user_info = userFeign.getUserInfo(sendTo);
                 //用户信息
                 messageVo.setTitle(user_info.getNickname());
                 messageVo.setImg(user_info.getAvatarUrl());
                 //消息内容
                 messageVo.setContent(msg.getMessageText());
                 messageVo.setDate( msg.getCreateTime());
                 data.getMessages().add(messageVo);
             }

         }

         return Result.success("获取消息队列成功！",data);
     }
}
